<template>
  <div class="app-container">
    <!-- 农资发放或退还管理-管理按钮 -->
    <div class="search-bar">
      <el-input v-model="searchData.dh" placeholder="请输入领用单号查询" />
      <el-input v-model="searchData.nhxm" placeholder="请输入农户姓名查询" />
      <el-input v-model="searchData.dkmc" placeholder="请输入地块名称查询" />
      <TimeFrame
        :start.sync="searchData.createTime1"
        :end.sync="searchData.createTime2"
      />
      <el-button
        v-waves
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="searchBtnHandle"
        >查询</el-button
      >

      <div style="float: right">
        <el-button
          v-waves
          type="primary"
          icon="el-icon-plus"
          @click="openAdd"
          size="small"
          v-permission="'jtnc-nzffthgl-krNzffthgl-add'"
          >添加新纪录
        </el-button>
      </div>
    </div>
    <!-- 农资发放或退还管理-列表 -->
    <el-table
      ref="dataTable"
      :data="tableData"
      stripe
      border
      @selection-change="handleTableSelectChange"
      v-loading="isLoading"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        header-align="center"
      />
      <el-table-column label="领用单号" prop="dh" align="center" />
      <el-table-column label="农资管理员" prop="nzglyxm" align="center" />
      <el-table-column label="农户" prop="nhxm" align="center" />
      <el-table-column label="地块" prop="dkmc" align="center" />
      <el-table-column label="农资" prop="nzmc" align="center" />
      <el-table-column label="数量" prop="nzsl" align="center" />
      <el-table-column label="领用时间" prop="createTime" align="center" />
      <el-table-column label="状态" prop="state" align="center">
        <template v-slot="scope">
          {{
            scope.row.state == 0
              ? "未确认"
              : scope.row.state == 1
              ? "已确认"
              : "已拒绝"
          }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120" align="center">
        <template v-slot="scope">
          <el-button
            type="text"
            style="color: #13ce66"
            size="small"
            @click="openView(scope.row)"
            >详情</el-button
          >
          <el-button
            v-permission="'jtnc-nzffthgl-krNzffthgl-update'"
            type="text"
            size="small"
            @click="openUpdate(scope.row)"
            >修改
          </el-button>
          <!-- <el-button
            v-permission="'jtnc-nzffthgl-krNzffthgl-delete'"
            style="color: #ff6d6d"
            type="text"
            size="small"
            @click="deleteByIds(scope.row)"
            >删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 农资发放或退还管理-分页 -->
    <el-pagination
      style="text-align: center; margin-top: 10px"
      layout="total,prev,pager,next,sizes,jumper"
      :page-size="pager.limit"
      :current-page="pager.page"
      :total="pager.totalCount"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <!-- 添加修改弹窗 -->
    <el-dialog
      :title="titleMap[dialogType]"
      :close-on-click-modal="dialogType !== 'view' ? false : true"
      :visible.sync="dialogFormVisible"
      @close="resetTemp"
      width="800px"
      :key="'myDialog' + dialogIndex"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="100px"
        :disabled="dialogType === 'view'"
      >
        <el-form-item
          label="领用单号"
          prop="dh"
          :rules="[]"
          v-if="dialogType != 'add'"
        >
          <el-input
            v-model="temp.dh"
            placeholder="请输入领用单号"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item
          label="地块"
          prop="dkglId"
          :rules="[{ required: true, message: '请输入地块', trigger: 'blur' }]"
        >
          <el-select
            v-model="temp.dkglId"
            placeholder="请选择所属地块"
            @change="getDkxx"
          >
            <el-option
              v-for="item in dkList"
              :key="item.dkglId"
              :value="item.dkglId"
              :label="item.dkmc"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="农户"
          prop="nhxm"
          :rules="[
            { required: true, message: '请输入农户姓名', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="temp.nhxm"
            placeholder="请输入农户姓名"
            :disabled="true"
          />
        </el-form-item>

        <el-form-item
          label="农资"
          prop="nzId"
          :rules="[{ required: true, message: '请选择农资', trigger: 'blur' }]"
        >
          <el-select v-model="temp.nzId" placeholder="请选择农资">
            <el-option
              @click.native="temp.nzmc = item.nzmc"
              v-for="item in nzList"
              :key="item.nzId"
              :value="item.nzId"
              :label="item.nzmc"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="农资数量"
          prop="nzsl"
          :rules="[
            { type: 'number', message: '必须为数字' },
            { required: true, message: '请输入农资数量', trigger: 'blur' },
          ]"
        >
          <el-input-number
            v-model.number="temp.nzsl"
            placeholder="请输入农资数量"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-waves
          type="primary"
          v-if="dialogType !== 'view'"
          @click="saveData"
          >保存</el-button
        >
        <el-button v-waves @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from "@/directive/waves";
import crudMixin from "@/mixin/crudMixin";
import request from "@/utils/request";
import TimeFrame from "@/components/TimeFrame/index.vue";
export default {
  directives: { waves },
  mixins: [crudMixin],
  components: { TimeFrame },
  data() {
    return {
      url: {
        list: "/jtnc/nzffthgl/krNzffthgl/list",
        add: "/jtnc/nzffthgl/krNzffthgl/add",
        info: "/jtnc/nzffthgl/krNzffthgl/getById",
        update: "/jtnc/nzffthgl/krNzffthgl/update",
        delete: "/jtnc/nzffthgl/krNzffthgl/delete",
        export: "/jtnc/nzffthgl/krNzffthgl/export/excel",
        dk: "/jtnc/dkgl/krDkgl/select",
        dkxx: "/jtnc/zzjh/krZzjh/getZzjhByDkId",
        nz: "/jtnc/nzxx/krNzxx/select",
      },
      dkList: [],
      nzList: [],
      needGetParam: true,
      idName: "nzffId",
    };
  },
  created() {
    this.$set(this.searchData, "type", 1);
    this.loadTableList();
    this.resetTemp();
  },
  methods: {
    getdkList() {
      request({
        url: this.url.dk,
        method: "get",
      }).then((res) => {
        if (res.code == 200) {
          this.dkList = res.data;
        }
      });
    },
    getnzList() {
      request({
        url: this.url.nz,
        method: "get",
      }).then((res) => {
        if (res.code == 200) {
          this.nzList = res.data;
        }
      });
    },
    getSelectData() {
      this.getdkList();
      this.getnzList();
      this.$set(this.temp, "type", 1);
    },
    getDkxx(val) {
      request({
        url: this.url.dkxx,
        method: "get",
        params: {
          dkglId: val,
        },
      }).then((res) => {
        if (res.code == 200) {
          this.$set(this.temp, "dkmc", res.data.dkmc);
          this.$set(this.temp, "nhxm", res.data.nhxm);
          this.$set(this.temp, "nhId", res.data.nhId);
          this.$set(this.temp, "zzjhId", res.data.zzjhId);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.search-bar {
  margin-bottom: 10px;
  ::v-deep {
    .el-input,
    .el-select {
      width: 180px;
      margin-right: 10px;
      margin-bottom: 5px;
    }
  }
}
</style>
